import add from './medal-add.js'
import del from './medal-del.js'

let datagrid = document.querySelector("._datagrid");

let searchForm = document.forms.searchForm;
let seId = searchForm.seId; 

fetch("SportingEvents-list.let").then(r=>r.json()).then(data=>{
		data.forEach(item=>{
			seId.add(new Option(item.seName,item.seId));
		})
	});

let ms = ["","金","银","铜"];
let loadData = function() {
	fetch("medal-list.let").then((r) => r.json()).then((data) => {
		console.debug(data);
		datagrid.innerHTML = "";
		data.forEach((item, index) => {
			let tr = document.createElement("tr");
			let year = new Date(item.medalDate).getFullYear();
			let mounth = new Date(item.medalDate).getMonth() + 1;
			let date = new Date(item.medalDate).getDate();
			let dd = year + "-" + mounth + "-" + date;
			tr.innerHTML = `
				<td>${item.seName}</td>
				<td>${item.countryName}</td>
				<td>${item.sportsmanName}</td>
				<td>${ms[item.medalNum]}</td>
				<td>${item.medalResult}</td>
				<td>${dd}</td>
				<td class="p-2">
					<div class="btn-group btn-group-sm">
						<button type="button" class="btn btn-danger _delete" title="删除">
							<i class="fa fa-remove"></i>
						</button>
					</div>
				</td>`;
			datagrid.appendChild(tr);
			tr.querySelector("._delete").onclick = function() {
				del(item);
			}
		});
	}).catch((e) => {
		console.error(e);
	});
}
loadData();
window.addEventListener("message", function(e) {
	console.debug(e);
	if (e.data == "loadData") {
		loadData();
	}
});
add();